package com.iemes.controller.equip;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.EquipAccepFormMap;
import com.iemes.entity.EquipApplyFormMap;
import com.iemes.entity.ErpEquipmentapply;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.DepartmentMapper;
import com.iemes.mapper.equip.EquipAccepMapper;
import com.iemes.mapper.equip.EquipApplyMapper;
import com.iemes.plugin.PageView;
import com.iemes.upload.UploadUtil;
import com.iemes.util.Common;

@RequestMapping("equip")
@Controller
public class EquipApplyController extends BaseController {

	@Inject
	private EquipApplyMapper equipMapper;

	@Inject
	private DepartmentMapper departmentMapper;
	
	@Inject
	private EquipAccepMapper accepMapper;

	@RequestMapping(value = "equip_apply", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<EquipApplyFormMap> findByWhere = equipMapper
				.findByWhere(new EquipApplyFormMap());
		List<ResFormMap> findByRes = findByRes();
		List<DepartmentForMap> findByAll = departmentMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("equipbom", findByWhere);
		model.addAttribute("res", findByRes);
		model.addAttribute("findByAll", findByAll);
		return Common.BACKGROUND_PATH + "/equip/apply/equipbom_list";
	}

	@ResponseBody
	@RequestMapping("equip_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		EquipApplyFormMap equipApplyFormMap = getFormMap(EquipApplyFormMap.class);
		if (equipApplyFormMap.get("states")==null) {
			equipApplyFormMap.put("states", 1);
		}
		equipApplyFormMap = toFormMap(equipApplyFormMap, pageNow, pageSize,
				equipApplyFormMap.getStr("orderby"));
		List<EquipApplyFormMap> findByPage = equipMapper.findByPage(equipApplyFormMap);
		pageView.setRecords(findByPage);
		return pageView;
	}

	@RequestMapping("equip_adds")
	public String addUI(Model model, HttpServletRequest request)
			throws Exception {
		// 查询所有菜单
		DepartmentForMap departmentForMap = getFormMap(DepartmentForMap.class);
		List<DepartmentForMap> findByAll = departmentMapper
				.findByNames(departmentForMap);
		model.addAttribute("equipall", findByAll);

		return Common.BACKGROUND_PATH + "/equip/apply/equipbom_add";
	}

	@ResponseBody
	@RequestMapping("equipbom_addEntity")
	// @Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "设备采购申请单", methods = "设备-新增设备")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(
			@Validated ErpEquipmentapply equipmentapply,
			BindingResult bindingResult,
			@RequestParam(value = "pricePic", required = false) MultipartFile file,
			HttpServletRequest request) throws Exception {
		
		if (!file.isEmpty()) {
			// 类型的验证
			// 原文件名（包含后缀名）
			String originalFilename = file.getOriginalFilename();
			// 后缀名
			String getUploadContentType = originalFilename
					.substring(originalFilename.lastIndexOf(".") + 1);
			UploadUtil uploadUtil = new UploadUtil();
			uploadUtil.uploadImage2(request, file, getUploadContentType,
					originalFilename);
			String imagePath2 = uploadUtil.getImagePath2();
			String imageSrc = "<img onclick='ok(this)' height='30' width='30' src='http://localhost:9100/TY-ERP"
					+ imagePath2 + "'/>";
			equipmentapply.setPricePic(imageSrc);

		}

		// 获取request
		request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		equipmentapply.setStatus(1);
		equipmentapply.setDate(sdf.format(date));
		equipmentapply.setAcceptanceStatus(2);
		equipmentapply.setFillpeopleid((Integer) userFormMap.get("id"));
		
		try {
			equipMapper.insertSelective(equipmentapply);
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "equip_deleteEntity")
	@SystemLog(module = "设备采购申请单", methods = "设备-删除设备")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		EquipApplyFormMap equipApplyFormMap = getFormMap(EquipApplyFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();

		for (int i = 0; i < str.length; i++) {
			equipApplyFormMap.set("status", 2);
			equipApplyFormMap.put("id", str[i]);
			equipApplyFormMap.put("deta", sdf.format(date));
			equipMapper.editEntity(equipApplyFormMap);
		}

		return "success";
	}

	@RequestMapping("equip_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		// 查询所有菜单
		DepartmentForMap departmentForMap = getFormMap(DepartmentForMap.class);
		List<DepartmentForMap> findByAll = departmentMapper
				.findByNames(departmentForMap);
		EquipApplyFormMap findbyFrist = equipMapper.findbyFrist("id", id,
				EquipApplyFormMap.class);
		model.addAttribute("equipapp", findByAll);
		model.addAttribute("equipAll", findbyFrist);
		return Common.BACKGROUND_PATH + "/equip/apply/equipbom_edit";
	}

	@ResponseBody
	@RequestMapping(value = "equip_editEntity")
	@SystemLog(module = "设备采购申请单", methods = "设备-修改设备")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(
			@Validated ErpEquipmentapply equipmentapply,
			BindingResult bindingResult,
			@RequestParam(value = "pricePic", required = false) MultipartFile file,
			HttpServletRequest request) throws Exception {
		String imageSrc =null;
		if (!file.isEmpty()) {
			// 类型的验证
			// 原文件名（包含后缀名）
			String originalFilename = file.getOriginalFilename();
			// 后缀名
			String getUploadContentType = originalFilename
					.substring(originalFilename.lastIndexOf(".") + 1);
			UploadUtil uploadUtil = new UploadUtil();
			uploadUtil.uploadImage2(request, file, getUploadContentType,
					originalFilename);
			String imagePath2 = uploadUtil.getImagePath2();
			imageSrc = "<img onclick='ok(this)' height='30' width='30' src='http://localhost:9100/TY-ERP"
					+ imagePath2 + "'/>";
		}
		// 获取request
		request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		equipmentapply.setDate(sdf.format(date));
		equipmentapply.setFillpeopleid((Integer) userFormMap.get("id"));				
		equipMapper.updateByPrimaryKeySelective(equipmentapply);	
		return "success";
	}
	
	@ResponseBody
	@Transactional(readOnly=false)
	@RequestMapping(value = "equip_accepEntity")
	@SystemLog(module = "设备采购申请单", methods = "设备-验收设备")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String accepEntity() throws Exception {
		EquipApplyFormMap equipApplyFormMap = getFormMap(EquipApplyFormMap.class);
		EquipAccepFormMap equipAccepFormMap =getFormMap(EquipAccepFormMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
						.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);		
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		for (int i = 0; i < str.length; i++) {
			equipApplyFormMap.set("acceptanceStatus", 1);
			equipApplyFormMap.put("id", str[i]);
			equipApplyFormMap.put("date", sdf.format(date));		
			equipMapper.editEntity(equipApplyFormMap);
			//向验收表插入数据		
			equipAccepFormMap.put("date", date);
			equipAccepFormMap.put("equipmentApplyId", str[i]);
			equipAccepFormMap.put("fillPeopleId", userFormMap.get("id"));
			accepMapper.addEntity(equipAccepFormMap);
			equipAccepFormMap.clear();
		}

		return "success";
	}
	
}
